package com.google.code.virtualhockey.vhx.sds.fields

import com.google.code.virtualhockey.vhx.sds._
import com.google.appengine.api.datastore.Text
import com.google.appengine.api.datastore.ShortBlob
import com.google.appengine.api.datastore.Blob

/**
 * Long text string with a length of up to 1 MB.
 */
class SLongText( parent: STMetaData[_],
                 name: String,
                 dbName: Option[String] = None ) extends SField( parent, name, false, dbName ) {

  protected type TRawType = String
  protected type TGaeType = Text
  protected val clsRaw = classOf[TRawType]
  protected val clsGae = classOf[TGaeType]

  override protected[sds] def convertGaeToSds( value: TGaeType ): TRawType = {
    value.getValue
  }

  override protected[sds] def convertSdsToGae( value: TRawType ): TGaeType = {
    new Text( value )
  }
}

/**
 * Short text string with a maximum length of 500 characters.
 */
class SShortText( parent: STMetaData[_],
                  name: String,
                  indexed: Boolean = false,
                  dbName: Option[String] = None ) extends SField( parent, name, indexed, dbName ) {

  protected type TRawType = String
  protected type TGaeType = String
  protected val clsRaw = classOf[TRawType]
  protected val clsGae = classOf[TGaeType]

  override protected def validate( value: Option[TRawType] ): Unit = {
    if ( value != None && value.get.length > 500 )
      Predef.error( "A String persisted as short text cannot be longer than " + 500 + " characters." )
  }
}